import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:jinke/global/user/logic.dart';
import 'package:jinke/page/pro_list/subs/pro_detail/model/pro_com_model.dart';
import 'package:jinke/utils/httputils/api.dart';
import 'package:jinke/utils/httputils/resultdata.dart';
import 'package:jinke/utils/util.dart';

import '../../../../../../global/user/user_info.dart';
import '../../../../../../utils/component/dialog/bottom.dart';
import 'index.dart';

class ProOpenStep1Controller extends GetxController {
  ProOpenStep1Controller();

  final state = ProOpenStep1State();

  Future<void> getUserList() async {
    ResultData res = await MeApi.getUserList();
    if (res.isSuccess) {
      List data = res.data;
      state.users = data.map((e) => UserInfo.fromJson(e)).toList();
    }
  }

  void actionLead() async {
    if (state.users == null) {
      await getUserList();
    }
    showActionSheet(
      titles: state.users?.map((e) => e.userName ?? '').toList() ?? [],
      choosedIndexs: [state.leadIndex],
      onTap: (p0) {
        state.leadIndex = p0.first;
        state.lead = state.users![p0.first];
        update();
      },
    );
  }

  void actionChooseCom() async {
    await getAllComs();
    var page = BrnSingleSelectCityPage(
      showSearchBar: true,
      hotCityTitle: 'TOP',
      hotCityList: state.hotCitys ?? [],
      cityList: state.allCitys,
      locationText: state.comMod?.name ?? '',
      onValueChanged: (value) {
        state.com = state.coms
            ?.firstWhere((element) => element.enterpriseAlias == value.name);
        state.comMod = value;
        update();
      },
    );
    Get.to(() => page);
  }

  Future<void> getAllComs() async {
    if (state.hotCitys != null) {
      return;
    }
    ResultData res = await ProListApi.getComList();
    if (res.isSuccess) {
      state.coms = [];
      List data = res.data;
      List hot = data.first['val'];
      state.hotCitys = hot.map((e) => getCityMod(e)).toList();
      data.removeAt(0);
      List all = data.map((e) => e['val']).toList();
      state.allCitys = [];
      all.forEach((element) {
        List et = element;

        List<BrnSelectCityModel> _list = et.map((e) => getCityMod(e)).toList();
        state.allCitys!.addAll(_list);
      });
    }
  }

  BrnSelectCityModel getCityMod(Map<String, dynamic> map) {
    var com = ProComModel.fromJson(map);
    state.coms?.add(com);
    return BrnSelectCityModel(
        name: com.enterpriseAlias ?? '', cityCode: com.enterpriseType ?? '');
  }

  Future<bool> onNext() async {
    if (state.nameCtrl.text.isEmpty) {
      EasyLoading.showToast('请输入项目全称');
      return false;
    }

    if (state.comMod == null) {
      EasyLoading.showToast('请选择企业');
      return false;
    }

    if (state.buildCtrl.text.isEmpty) {
      EasyLoading.showToast('请输入建设内容');
      return false;
    }

    if (state.putMoney <= 0) {
      EasyLoading.showToast('请添加投资金额');
      return false;
    }

    if (state.putPeople <= 0) {
      EasyLoading.showToast('请添加用工人数');
      return false;
    }
    state.params = {
      'employmentNumber': state.putPeople,
      'investmentQuota': state.putMoney,
      'projectAlias':
          (state.com?.enterpriseAlias ?? '') + (state.buildCtrl.text),
      'projectCommissioner': [
        {
          'id': UserController().state.user?.userId ?? '',
          'name': UserController().state.user?.userName ?? ''
        }
      ],
      'projectContent': state.comMod?.name ?? '',
      'projectContentAlias': state.buildCtrl.text,
      'projectName': state.nameCtrl.text,
      'projectType': "",
      'remark': state.remarkCtrl.text,
    };
    return true;
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    // BrnSingleSelectCityPage(hotCityList: hotCityList)
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}


/**
 BrnSingleSelectCityPage({
    this.appBarTitle = '',
    this.hotCityTitle = '',
    this.hotCityList,
    this.cityList,
    this.showSearchBar = true,
    this.locationText = '',
    this.onValueChanged,
  });
 */